package com.guigu.commons.pojo;

import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.stereotype.Component;

import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * @ClassName:SecurityUser
 * @Description:security框架中的实体类对象
 * @Author:longtao
 * @Date:2021/6/28
 * @Version:1.0
 */

public class SecurityUser implements UserDetails {
    private String id;
    private String account;
    private String password;
    private Set<String> permissions;

    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        Set<SimpleGrantedAuthority> permission = new HashSet<>();
        permissions.stream().forEach(s -> {
            permission.add(new SimpleGrantedAuthority(s));
        });
        return permission;
    }

    public SecurityUser() {
    }

    public SecurityUser(String account, String password, Set<String> permissions) {
        this.account = account;
        this.password = password;
        this.permissions = permissions;
    }

    public SecurityUser(String id, String account, String password, Set<String> permissions) {
        this.id = id;
        this.account = account;
        this.password = password;
        this.permissions = permissions;
    }

    @Override
    public String getPassword() {
        return password;
    }

    @Override
    public String getUsername() {
        return account;
    }

    @Override
    public boolean isAccountNonExpired() {
        return true;
    }

    @Override
    public boolean isAccountNonLocked() {
        return true;
    }

    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }

    @Override
    public boolean isEnabled() {
        return true;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getAccount() {
        return account;
    }

    public void setAccount(String account) {
        this.account = account;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public Set<String> getPermissions() {
        return permissions;
    }

    public void setPermissions(Set<String> permissions) {
        this.permissions = permissions;
    }
}
